﻿using System.Collections.Generic;
using System.Linq;
using DoDoveFramework;
using UnityEngine;
using UnityEngine.Tilemaps;

namespace Script
{
    public class TileMapBuilder : BaseBuilder<GameObject, TileMapBuilder>
    {
    }

    #region 创建grid

    public static class TileMapGridCreateGridExtend
    {
        public static TileMapBuilder CreateGrid(this TileMapBuilder tileMapBuilder)
        {
            return tileMapBuilder.Do(go =>
                {
                    go.AddComponent<Grid>();

                    var grid = go.GetComponent<Grid>();
                    grid.cellSize = new Vector3(1, 1, 1);
                    grid.cellGap = Vector3.zero;
                    grid.cellLayout = GridLayout.CellLayout.Rectangle;
                    grid.cellSwizzle = GridLayout.CellSwizzle.XYZ;
                }
            );
        }
    }

    #endregion

    #region 创建tilemap 添加到tilemap root节点下

    public static class TileMapGridCreateTileMapExtend
    {
        public static TileMapBuilder AddTileMap(this TileMapBuilder tileMapBuilder, Vector3 tilemapPos,
            string tilemapName = "DataTilemap")
        {
            return tileMapBuilder.Do(go =>
                {
                    var tileMap = new GameObject("TileMap");
                    var tilemapComponent = tileMap.AddComponent<Tilemap>();
                    var tilemapRender = tileMap.AddComponent<TilemapRenderer>();
                    tileMap.transform.SetParent(go.transform);
                    tileMap.transform.localPosition = tilemapPos;
                    tileMap.name = tilemapName;
                    tilemapRender.sortingLayerName = "Ground";
                }
            );
        }
    }

    #endregion

    #region 创建噪声目标纹理

    public static class TileMapGridAddNoiseTargetGoExtend
    {
        public static TileMapBuilder AddNoiseTargetGo(this TileMapBuilder tileMapBuilder, Vector3 tilemapPos)
        {
            return tileMapBuilder.Do(go =>
                {
                    var noiseTarget = new GameObject("NoiseTarget");
                    noiseTarget.transform.SetParent(go.transform);
                    noiseTarget.transform.localPosition = tilemapPos;
                    noiseTarget.AddComponent<MeshRenderer>();
                }
            );
        }
    }

    #endregion


    #region 解析define placeholder 的Res和对应的类型

    public static class TileMapGridInitPlaceholderExtend
    {
        public static TileMapBuilder InitPlaceholder(this TileMapBuilder tileMapBuilder,
            Dictionary<string, int> placeholderDict)
        {
            return tileMapBuilder.Do(go =>
                {
                    //配置数据
                    var placeholder = GameConfigKit.Instance.gameConfig.TbDefine.Placeholder;
                    var placeholderRes = GameConfigKit.Instance.gameConfig.TbDefine.PlaceholderRes;
                    for (int i = 0; i < placeholderRes.Count; i++)
                    {
                        placeholderDict[placeholderRes[i]] = placeholder[i];
                    }
                }
            );
        }
    }

    #endregion

    #region 解析define gridWeight  方便后续查找对应的值

    public static class TileMapGridWeightExtend
    {
        public static TileMapBuilder InitGridWeight(this TileMapBuilder tileMapBuilder, List<float> bounds)
        {
            return tileMapBuilder.Do(go =>
                {
                    //配置数据
                    var gridWeight = GameConfigKit.Instance.gameConfig.TbDefine.GridWeight;

                    HashSet<float> boundsSet = new HashSet<float>();
                    foreach (var weight in gridWeight)
                    {
                        boundsSet.Add(weight.X);
                        boundsSet.Add(weight.Y);
                    }

                    // 转换为数组并排序（升序）
                    bounds.AddRange(boundsSet.OrderBy(b => b).ToList());
                }
            );
        }
    }

    #endregion

    #region 加载computer shader

    public static class TileMapGridInitComputerShaderExtend
    {
        public static TileMapBuilder InitComputerShader(this TileMapBuilder tileMapBuilder,
            ComputeShader perlinNoiseShader)
        {
            return tileMapBuilder.Do(go =>
            {
                perlinNoiseShader = ResKit.Instance.GetResByResName<ComputeShader>("Grid/Map", "PerlinNoise");

                var res = ResKit.Instance.GetResByResName<ComputeShader>("Grid/Map", "PerlinNoise");

                if (res != null) Debug.Log("资源加载成功");
            });
        }
    }

    #endregion
}